(function() {

    var appid = getQueryString('appid')
    var orderuuid = getQueryString('orderuuid')
    // 请求参数
    var requestParams = {
        orderUuid: orderuuid
    }
    var gift=jck.ajax;

    // 配置
    var config = {
        url: '/api/v1/common/tripSharing',
        type: 'post',
        params: requestParams,
        successCallBack:function(res){
            $('#isBody').show()
            var data = res.data || {}
            $('.mask').hide()
            if(!data.mainStatus){ // 超时
                $('.wrapper-on').hide()
                $('#successorder').show()
                return;
            }else if(data.mainStatus===5){ // 取消
                $('.wrapper-on').hide()
                $('#cancel').show()
                return;
            }else { // 正常
                $('.wrapper-off').hide()
                $('.wrapper-on').show()
            }
            var topInfoData = {
                name: data.name,
                score: data.score,
                orderCount: data.orderCount,
                plateNum: data.plateNum,
                carColor: data.carColor,
                model: data.model,
                face: data.face || base + '/activity/tripShare/images/driver_pic.png'
            };
            var topInfohtml = template("topInfo", topInfoData);
            document.getElementsByClassName('topInfo')[0].innerHTML = topInfohtml
            var pasArrTime={
                100:'待上车',
                200:'待上车',
                210:'待上车',
                220:'待上车',
                300:data.pasArrTime?formatHHMM(data.pasArrTime)+'上车':'',
                400:data.pasArrTime?formatHHMM(data.pasArrTime)+'上车':'',
                500:data.pasArrTime?formatHHMM(data.pasArrTime)+'上车':'',
                600:data.pasArrTime?formatHHMM(data.pasArrTime)+'上车':'',
                610:data.pasArrTime?formatHHMM(data.pasArrTime)+'上车':'',
            }
            var startEndInfoData = {
                originDetailAddress: data.originDetailAddress,
                pasArrTime: pasArrTime[data.subStatus] || '', // 上车时间
                arriveTime:data.arriveTime?formatHHMM(data.arriveTime)+'下车':'',// 下车时间
                destDetailAddress: data.destDetailAddress
            };



            var startEndInfoHtml = template("startEndInfo", startEndInfoData);
            document.getElementsByClassName('startEndInfo')[0].innerHTML = startEndInfoHtml

            var carPos = null, pointList = []
            if (data.passengerStatus === 0) {
                carPos = [data.driverLocation.coordinate.lng, data.driverLocation.coordinate.lat]
            } else {
                if(data.driverServiceProcessPointList){
                    data.driverServiceProcessPointList.forEach((item, index) => {
                        pointList.push([item.coordinate.lng, item.coordinate.lat])
                    })
                    carPos = [pointList[pointList.length - 1][0], pointList[pointList.length - 1][1]]
                }

            }
            console.log(pointList, '轨迹数组')
            console.log(data)
            // 实例化地图对象
            var map = new AMap.Map('map-container');
            // 起点标注内容，包括冒泡框和起点文字
            var startLabel = data.passengerStatus === 1 && `<div class="start-label"><div class="label-box"><p class="p1"><span class="left">距离终点 </span><span class="right">${data.remainingDistance}公里</span></p><p class="p2"><span class="left">预计行驶 </span><span class="right">${data.estimatedDrivingTime}分钟</span></p></div></div>`
            // 起点、终点、小车标记、折线配置
            if (!StartMarkerOptions && !EndMarkerOptions) {

            }
            var StartMarkerOptions = {
                map,
                position: [data.originLng, data.originLat],  //起点经纬度
                offset: new AMap.Pixel(-15, -15),
                icon: base+'/activity/tripShare/images/map_icon_start.png',
                label: { content: startLabel, offset: new AMap.Pixel(-61, -68) }
            }, EndMarkerOptions = {
                map,
                position: [data.destLng, data.destLat], //终点经纬度
                offset: new AMap.Pixel(-15, -15),
                icon: base+'/activity/tripShare/images/map_icon_end.png'
            }, CarMarkerOptions = {
                map,
                angle: data.angle,  //小车旋转角度
                position: carPos, //小车经纬度
                offset: new AMap.Pixel(-15, -10),
                icon: base+'/activity/tripShare/images/map_icon_blackcar.png'
            }, PolylineOptions = {
                map,
                strokeColor: '#9EA7B1',
                isOutline: true,
                borderWeight: 2,
                strokeWeight: 4,
                outlineColor: '#6B7886',
                strokeOpacity: 1,
                lineJoin: 'round',
                lineCap: 'round',
                path: pointList  //轨迹坐标数组
            }
            if (!startMarker && !endMarker) {
                var startMarker = new AMap.Marker(StartMarkerOptions)
                var endMarker = new AMap.Marker(EndMarkerOptions)
            }
            var carMarker = data.passengerStatus !== 2 && new AMap.Marker(CarMarkerOptions)
            var Polyline = data.passengerStatus !== 0 && new AMap.Polyline(PolylineOptions)
            if (data.passengerStatus !== 2) {
                // 5秒更新一次
                setTimeout(function () {
                    Request()
                }, 5000)
            }
        }

    }
    gift.ajax_md5(config)

    function formatHHMM(timestamp) {
        var time = new Date(timestamp);
        return (time.getHours() > 9 && time.getHours() || ('0' + time.getHours())) + ':' + (time.getMinutes() > 9 && time.getMinutes() || ('0' + time.getMinutes()))
    }
})();

